import { _decorator, Collider, Component, ITriggerEvent, Node, sp } from 'cc';
import { GameManager } from '../framework/GameManager';
import { Constant } from '../framework/Constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('BulletProp')
export class BulletProp extends Component {

    private propSpeed = 0.3;

    private propXSpeed = 0.3;

    private gameManager: GameManager;

    protected onEnable(): void {
        const collider = this.getComponent(Collider);
        collider.on('onTriggerEnter', this.triggerEnter, this);
    }
    protected onDestroy(): void {
        const collider = this.getComponent(Collider);
        collider.off('onTriggerEnter', this.triggerEnter, this);
    }
    start() {

    }



    triggerEnter(event: ITriggerEvent) {
        const name = event.selfCollider.node.name;
        if (name === 'bulletM') {
            this.gameManager.changeBulletType(Constant.BulletPropType.BULLET_M);
        } else if (name === 'bulletS') {
            this.gameManager.changeBulletType(Constant.BulletPropType.BULLET_S);
        } else if (name === 'bulletH') {
            this.gameManager.changeBulletType(Constant.BulletPropType.BULLET_H);
        }
        // this.node.destroy();
PoolManager.instance().put(this.node);

    }

    update(deltaTime: number) {
        const pos = this.node.position;

        if (pos.x >= 20) {
            this.propXSpeed = -this.propSpeed;
        } else if (pos.x <= -20) {
            this.propXSpeed = this.propSpeed;
        }
        const z = pos.z + this.propSpeed;
        this.node.setPosition(pos.x + this.propXSpeed, pos.y, z);
        if (z > 50) {
            PoolManager.instance().put(this.node);
        }
    }

    show(gameManager: GameManager, speed: number) {
        this.gameManager = gameManager;
        this.propSpeed = speed;
    }
}

